/**
 * 
 */
package it.unibo.refolding.pico;

import it.unibo.refolding.common.MyRuntimeException;

import java.lang.reflect.Field;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;


public class CommonClRefoldingOptions {
	private final Option clRangeStartNmOption, clRangeEndNmOption;

	private CommonClRefoldingOptions(Option clRangeStartNmOption, Option clRangeEndNmOption) {
		this.clRangeStartNmOption = clRangeStartNmOption;
		this.clRangeEndNmOption = clRangeEndNmOption;
	}

	public void addTo(Options options) {
		for (Field field : getClass().getDeclaredFields()) {
			Option option;
			try {
				option = (Option) field.get(this);
			} catch (IllegalAccessException e) {
				throw new MyRuntimeException(e.getMessage(), e);
			}
			options.addOption(option);
		}
	}

	public static CommonClRefoldingOptions create() {
		Option clRangeStartNmOption = new Option("clRangeStartNm", true,
				"What distance above the surface we should hover.");
		clRangeStartNmOption.setRequired(true);
		clRangeStartNmOption.setType(Double.TYPE);

		Option clRangeEndNmOption = new Option("clRangeEndNm", true,
				"What distance above the surface we should hover.");
		clRangeEndNmOption.setRequired(true);
		clRangeEndNmOption.setType(Double.TYPE);

		return new CommonClRefoldingOptions(clRangeStartNmOption, clRangeEndNmOption);
	}

	public double getClRangeStartNm(CommandLine line) {
		return Double.parseDouble(line.getOptionValue(clRangeStartNmOption.getOpt()));
	}

	public double getClRangeEndNm(CommandLine line) {
		return Double.parseDouble(line.getOptionValue(clRangeEndNmOption.getOpt()));
	}

	public double clRangeStartM(CommandLine line) {
		return getClRangeStartNm(line) / 1e9;
	}

	public double clRangeEndM(CommandLine line) {
		return getClRangeEndNm(line) / 1e9;
	}
}
